/*
 * Copyright (c) 2024 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "nweb_engine_impl.h"
#include "cef/libcef/browser/devtools/devtools_manager_delegate.h"
#include "nweb_adsblock_manager_impl.h"
#include "nweb_cookie_manager_impl.h"
#include "nweb_data_base_impl.h"
#include "nweb_download_manager_impl.h"
#include "nweb_hilog.h"
#include "nweb_impl.h"
#include "nweb_key_event_impl.h"
#include "nweb_web_storage_impl.h"

namespace OHOS::NWeb {

static std::shared_ptr<NWebEngineImpl> g_nweb_engine_impl =
    std::make_shared<NWebEngineImpl>();

NWebEngineImpl::NWebEngineImpl() {
  nweb_data_base_ = std::make_shared<NWebDataBaseImpl>();
  nweb_web_storage_ = std::make_shared<NWebWebStorageImpl>();
  nweb_download_manager_ = std::make_shared<NWebDownloadManagerImpl>();
}

// static
std::shared_ptr<NWebEngine> NWebEngine::GetInstance() {
  return g_nweb_engine_impl;
}

std::shared_ptr<NWeb>
NWebEngineImpl::CreateNWeb(std::shared_ptr<NWebCreateInfo> create_info) {
  return NWebImpl::CreateNWeb(create_info);
}

std::shared_ptr<NWeb> NWebEngineImpl::GetNWeb(int32_t nweb_id) {
  return NWebImpl::GetNWeb(nweb_id);
}

std::shared_ptr<NWebDataBase> NWebEngineImpl::GetDataBase() {
  return nweb_data_base_;
}

std::shared_ptr<NWebWebStorage> NWebEngineImpl::GetWebStorage() {
  return nweb_web_storage_;
}

std::shared_ptr<NWebCookieManager> NWebEngineImpl::GetCookieManager() {
  return NWebCookieManagerImpl::GetInstance();
}

std::shared_ptr<NWebDownloadManager> NWebEngineImpl::GetDownloadManager() {
  return nweb_download_manager_;
}

void NWebEngineImpl::SetWebTag(int32_t nweb_id, const char *web_tag) {
  NWebImpl::SetWebTag(nweb_id, web_tag);
}

void NWebEngineImpl::InitializeWebEngine(
    std::shared_ptr<NWebEngineInitArgs> init_args) {
#if defined(OHOS_API_INIT_WEB_ENGINE)
  NWebImpl::InitializeWebEngine(init_args);
#endif // defined(OHOS_API_INIT_WEB_ENGINE)
}

void NWebEngineImpl::PrepareForPageLoad(const std::string &url,
                                        bool preconnectable,
                                        int32_t num_sockets) {
  return NWebImpl::PrepareForPageLoad(url, preconnectable, num_sockets);
}

void NWebEngineImpl::SetWebDebuggingAccess(bool isEnableDebug) {
  static bool isDebuggingEnabled = false;
  if (isEnableDebug && !isDebuggingEnabled) {
    CefDevToolsManagerDelegate::StartHttpHandler(nullptr);
    WVLOG_I("StartHttpHandler Enabled");
    isDebuggingEnabled = true;
  } else if (!isEnableDebug && isDebuggingEnabled) {
    CefDevToolsManagerDelegate::StopHttpHandler();
    WVLOG_I("StopHttpHandler Enabled");
    isDebuggingEnabled = false;
  }
}

void NWebEngineImpl::AddIntelligentTrackingPreventionBypassingList(
    const std::vector<std::string> &hosts) {
#ifdef OHOS_ITP
  NWebImpl::AddIntelligentTrackingPreventionBypassingList(hosts);
#endif
}

void NWebEngineImpl::RemoveIntelligentTrackingPreventionBypassingList(
    const std::vector<std::string> &hosts) {
#ifdef OHOS_ITP
  NWebImpl::RemoveIntelligentTrackingPreventionBypassingList(hosts);
#endif
}

void NWebEngineImpl::ClearIntelligentTrackingPreventionBypassingList() {
#ifdef OHOS_ITP
  NWebImpl::ClearIntelligentTrackingPreventionBypassingList();
#endif
}

void NWebEngineImpl::PauseAllTimers() {
  NWebImpl::PauseAllTimers();
}

void NWebEngineImpl::ResumeAllTimers() {
  NWebImpl::ResumeAllTimers();
}

void NWebEngineImpl::PrefetchResource(
    const std::shared_ptr<NWebEnginePrefetchArgs> &pre_args,
    const std::map<std::string, std::string> &additional_http_headers,
    const std::string &cache_key, const uint32_t &cache_valid_time) {
  return NWebImpl::PrefetchResource(pre_args, additional_http_headers,
                                    cache_key, cache_valid_time);
}

void NWebEngineImpl::SetRenderProcessMode(RenderProcessMode mode) {
  NWebImpl::SetRenderProcessMode(mode);
}

RenderProcessMode NWebEngineImpl::GetRenderProcessMode() {
  return NWebImpl::GetRenderProcessMode();
}

void NWebEngineImpl::ClearPrefetchedResource(const std::vector<std::string>& cache_key_list) {
  return NWebImpl::ClearPrefetchedResource(cache_key_list);
}

void NWebEngineImpl::WarmupServiceWorker(const std::string &url) {
  NWebImpl::WarmupServiceWorker(url);
}

void NWebEngineImpl::SetHostIP(
    const std::string &hostName, const std::string &address, int32_t aliveTime) {
  NWebImpl::SetHostIP(hostName, address, aliveTime);
}

void NWebEngineImpl::ClearHostIP(const std::string &hostName) {
  NWebImpl::ClearHostIP(hostName);
}

void NWebEngineImpl::EnableWholeWebPageDrawing() {
#if defined(OHOS_SOFTWARE_COMPOSITOR)
  NWebImpl::EnableWholeWebPageDrawing();
#endif
}

std::shared_ptr<NWebAdsBlockManager> NWebEngineImpl::GetAdsBlockManager() {
  return NWebAdsBlockManagerImpl::GetInstance();
}

void NWebEngineImpl::TrimMemoryByPressureLevel(int32_t memoryLevel) {
  NWebImpl::TrimMemoryByPressureLevel(memoryLevel);
}
} // namespace OHOS::NWeb
